package repository

import (
	"context"
	"mts/trans_srv/internal/domain"
	"mts/trans_srv/internal/repository/dao"
	"strconv"
)

type TranslationRepository interface {
	GetAllTranslationWithElementsId(ctx context.Context, ids ...string) ([]domain.Translation, error)
	GetAllElementsByLocale(ctx context.Context, locale string, ids ...string) ([]domain.Translation, error)
	Insert(ctx context.Context, translation domain.Translation) error
	Update(ctx context.Context, translation domain.Translation) error
	Delete(ctx context.Context, translation domain.Translation) error
	IsExist(ctx context.Context, elementID, locale string) error
	BatchTranslation(ctx context.Context, translations []domain.Translation) error
}

type GormTranslationRepository struct {
	dao dao.TranslationDao
}

func (g GormTranslationRepository) BatchTranslation(ctx context.Context, translations []domain.Translation) error {
	var list []dao.Translation
	for _, v := range translations {
		list = append(list, g.toEntity(v))
	}
	return g.dao.BatchTranslation(ctx, list)
}

// NewTranslationRepository 返回 TranslationRepository 接口的实现
func NewTranslationRepository(dao dao.TranslationDao) TranslationRepository {
	return GormTranslationRepository{
		dao: dao,
	}
}
func (g GormTranslationRepository) IsExist(ctx context.Context, elementID, locale string) error {
	return g.dao.IsExist(ctx, elementID, locale)
}

func (g GormTranslationRepository) Insert(ctx context.Context, translation domain.Translation) error {
	err := g.dao.Insert(ctx, g.toEntity(translation))
	if err != nil {
		return err
	}
	return nil
}

func (g GormTranslationRepository) Update(ctx context.Context, translation domain.Translation) error {
	err := g.dao.Update(ctx, g.toEntity(translation))
	if err != nil {
		return err
	}
	return nil
}

func (g GormTranslationRepository) Delete(ctx context.Context, translation domain.Translation) error {
	err := g.dao.Delete(ctx, g.toEntity(translation))
	if err != nil {
		return err
	}
	return nil
}

//

func (g GormTranslationRepository) GetAllElementsByLocale(ctx context.Context, locale string, ids ...string) ([]domain.Translation, error) {
	translations, err := g.dao.GetAllElementsByLocale(ctx, locale, ids...)
	if err != nil {
		return nil, err
	}
	var list []domain.Translation
	for _, v := range translations {
		list = append(list, g.entityToDomain(v))
	}
	return list, nil
}

// GetAllTranslationWithElementsId 根据元素ID和语言ID获取所有翻译
func (g GormTranslationRepository) GetAllTranslationWithElementsId(ctx context.Context, ids ...string) ([]domain.Translation, error) {
	translations, err := g.dao.GetAllTranslationWithElementsId(ctx, ids...)
	if err != nil {
		return nil, err
	}
	var list []domain.Translation
	for _, v := range translations {
		list = append(list, g.entityToDomain(v))
	}
	return list, nil
}

// entityToDomain 将 dao.Translation 转换为 domain.Translation
func (g GormTranslationRepository) entityToDomain(lan *dao.Translation) domain.Translation {
	return domain.Translation{
		Content:   lan.Content,
		ElementID: strconv.Itoa(lan.ElementID),
		Locale:    lan.Locale,
	}
}

func (g GormTranslationRepository) toEntity(t domain.Translation) dao.Translation {
	id, _ := strconv.Atoi(t.ElementID)
	return dao.Translation{
		Content:   t.Content,
		ElementID: id,
		Locale:    t.Locale,
	}
}
